import Vue from 'vue';
import VueRouter from 'vue-router';

// 路由的懒加载（按需加载 有缓存）
const Login = () => import('@/pages/Login');
const Home = () => import('@/pages/Home');
const Register = () => import('@/pages/Register');
const Search = () => import('@/pages/Search');
const Detail = () => import('@/pages/Detail')
const AddCartSuccess = () => import('@/pages/AddCartSuccess')

//  重写 Push
const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (to, onComplete = () => { }, onAbort = () => { }) {
  originPush.call(this, to, onComplete, onAbort)
}

//  重写 Replace
const originRelpace = VueRouter.prototype.replace
VueRouter.prototype.push = function (to, onComplete = () => { }, onAbort = () => { }) {
  originRelpace.call(this, to, onComplete, onAbort)
}

// 应用路由插件
Vue.use(VueRouter);

const router = new VueRouter({
  mode: 'history',  // 设置路由模式
  routes: [  // 配置路由表
    {
      path: '/Login',
      name: 'Login',
      component: Login,
      meta: {
        isHidden: true,
      },
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home,
    },
    {
      path: '/Register',
      name: 'Register',
      component: Register,
      meta: {
        isHidden: true,
      },
    },
    {
      path: '/Search/:searchKey?',
      name: 'Search',
      component: Search,
    },
    {
      path: '/Details/:skuId',
      name: 'Details',
      component: Detail
    },
    {
      path: "/addCartSuccess",
      component: AddCartSuccess,
      name: "addCartSuccess",
    },
    {
      path: '/',
      redirect: '/Home',
    },
  ],
});

// const oldPush = router.push
// router.push = function (...args) {
//   oldPush.apply(this, args).catch(() => {})
// }

export default router